﻿using SF.Common;
using SF.DBUtility;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace SF.DAL
{
    /// <summary>
    /// 数据访问类:bs_good_info
    /// </summary>
    public partial class bs_good_info
    {
        public bs_good_info()
        { }
        #region  BasicMethod
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(long id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from bs_good_info");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.BigInt)
			};
            parameters[0].Value = id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public long Add(Model.bs_good_info model)
        {
            int goodId = 0;
            using (SqlConnection conn = new SqlConnection(DbHelperSQL.connectionString))
            {
                conn.Open();
                using (SqlTransaction trans = conn.BeginTransaction())
                {
                    try
                    {
                        StringBuilder strSqlGood = new StringBuilder();
                        strSqlGood.Append("insert into bs_good_info(");
                        strSqlGood.Append("busnieseNum,appNum,templatesNum,typeNum,goodTypeId,name,description,stock,costPrice,sellPrice,deletePrice,sort,status,goodHot,lastTime,lastUserId,showImage,zanCount)");
                        strSqlGood.Append(" values (");
                        strSqlGood.Append("@busnieseNum,@appNum,@templatesNum,@typeNum,@goodTypeId,@name,@description,@stock,@costPrice,@sellPrice,@deletePrice,@sort,@status,@goodHot,@lastTime,@lastUserId,@showImage,@zanCount)");
                        strSqlGood.Append(";select @@IDENTITY");
                        SqlParameter[] parameters = {
					new SqlParameter("@busnieseNum", SqlDbType.NVarChar,50),
					new SqlParameter("@appNum", SqlDbType.NVarChar,50),
					new SqlParameter("@templatesNum", SqlDbType.NVarChar,50),
					new SqlParameter("@typeNum", SqlDbType.NVarChar,50),
					new SqlParameter("@goodTypeId", SqlDbType.BigInt,8),
					new SqlParameter("@name", SqlDbType.NVarChar,50),
					new SqlParameter("@description", SqlDbType.NVarChar,-1),
					new SqlParameter("@stock", SqlDbType.Int,4),
					new SqlParameter("@costPrice", SqlDbType.Decimal,9),
					new SqlParameter("@sellPrice", SqlDbType.Decimal,9),
					new SqlParameter("@deletePrice", SqlDbType.Decimal,9),
					new SqlParameter("@sort", SqlDbType.Int,4),
					new SqlParameter("@status", SqlDbType.NVarChar,50),
					new SqlParameter("@goodHot", SqlDbType.NVarChar,50),
					new SqlParameter("@lastTime", SqlDbType.DateTime),
					new SqlParameter("@lastUserId", SqlDbType.BigInt,8),
					new SqlParameter("@showImage", SqlDbType.NVarChar,-1),
					new SqlParameter("@zanCount", SqlDbType.Int,4)};
                        parameters[0].Value = model.busnieseNum;
                        parameters[1].Value = model.appNum;
                        parameters[2].Value = model.templatesNum;
                        parameters[3].Value = model.typeNum;
                        parameters[4].Value = model.goodTypeId;
                        parameters[5].Value = model.name;
                        parameters[6].Value = model.description;
                        parameters[7].Value = model.stock;
                        parameters[8].Value = model.costPrice;
                        parameters[9].Value = model.sellPrice;
                        parameters[10].Value = model.deletePrice;
                        parameters[11].Value = model.sort;
                        parameters[12].Value = model.status;
                        parameters[13].Value = model.goodHot;
                        parameters[14].Value = model.lastTime;
                        parameters[15].Value = model.lastUserId;
                        parameters[16].Value = model.showImage;
                        parameters[17].Value = model.zanCount;

                        object obj = DbHelperSQL.GetSingle(strSqlGood.ToString(), parameters);
                        goodId = int.Parse(obj.ToString());

                        #region 图片
                        if (model.goodImages != null)
                        {
                            foreach (Model.bs_good_image goodImage in model.goodImages)
                            {
                                StringBuilder strSqlImage = new StringBuilder();
                                strSqlImage.Append("insert into bs_good_image(");
                                strSqlImage.Append("busnieseNum,appNum,templatesNum,goodId,imageUrl,remark)");
                                strSqlImage.Append(" values (");
                                strSqlImage.Append("@busnieseNum,@appNum,@templatesNum,@goodId,@imageUrl,@remark)");
                                SqlParameter[] imageParameters = {
					                new SqlParameter("@busnieseNum", SqlDbType.NVarChar,50),
					                new SqlParameter("@appNum", SqlDbType.NVarChar,50),
					                new SqlParameter("@templatesNum", SqlDbType.NVarChar,50),
					                new SqlParameter("@goodId", SqlDbType.BigInt,8),
					                new SqlParameter("@imageUrl", SqlDbType.NVarChar,-1),
					                new SqlParameter("@remark", SqlDbType.NVarChar,-1)};
                                imageParameters[0].Value = goodImage.busnieseNum;
                                imageParameters[1].Value = goodImage.appNum;
                                imageParameters[2].Value = goodImage.templatesNum;
                                imageParameters[3].Value = goodId;
                                imageParameters[4].Value = goodImage.imageUrl;
                                imageParameters[5].Value = goodImage.remark;

                                DbHelperSQL.ExecuteSql(conn, trans, strSqlImage.ToString(), imageParameters);
                                strSqlImage.Length = 0;
                            }
                        }
                        #endregion

                        #region 商品扩展
                        if (model.goodExtends != null)
                        {
                            foreach (Model.bs_good_extend goodExtend in model.goodExtends)
                            {
                                StringBuilder strSqlExtend = new StringBuilder();
                                strSqlExtend.Append("insert into bs_good_extend(");
                                strSqlExtend.Append("busnieseNum,appNum,templatesNum,goodId,goodTypeId,goodTypeFieldCode,fieldValue)");
                                strSqlExtend.Append(" values (");
                                strSqlExtend.Append("@busnieseNum,@appNum,@templatesNum,@goodId,@goodTypeId,@goodTypeFieldCode,@fieldValue)");
                                SqlParameter[] extendParameters = {
					                new SqlParameter("@busnieseNum", SqlDbType.NVarChar,50),
					                new SqlParameter("@appNum", SqlDbType.NVarChar,50),
					                new SqlParameter("@templatesNum", SqlDbType.NVarChar,50),
					                new SqlParameter("@goodId", SqlDbType.BigInt,8),
					                new SqlParameter("@goodTypeId", SqlDbType.BigInt,8),
					                new SqlParameter("@goodTypeFieldCode", SqlDbType.NVarChar,50),
					                new SqlParameter("@fieldValue", SqlDbType.NVarChar,-1)};
                                extendParameters[0].Value = goodExtend.busnieseNum;
                                extendParameters[1].Value = goodExtend.appNum;
                                extendParameters[2].Value = goodExtend.templatesNum;
                                extendParameters[3].Value = goodId;
                                extendParameters[4].Value = goodExtend.goodTypeId;
                                extendParameters[5].Value = goodExtend.goodTypeFieldCode;
                                extendParameters[6].Value = goodExtend.fieldValue;

                                DbHelperSQL.ExecuteSql(conn, trans, strSqlExtend.ToString(), extendParameters);
                                strSqlExtend.Length = 0;
                            }
                        }
                        #endregion

                        #region 商品返佣设置
                        if (model.goodReturnScale != null)
                        {
                            StringBuilder strSqlReturnScale = new StringBuilder();
                            strSqlReturnScale.Append("insert into bs_good_returnScale(");
                            strSqlReturnScale.Append("busnieseNum,appNum,templatesNum,goodId,level1ReturnScale,level2ReturnScale,level3ReturnScale)");
                            strSqlReturnScale.Append(" values (");
                            strSqlReturnScale.Append("@busnieseNum,@appNum,@templatesNum,@goodId,@level1ReturnScale,@level2ReturnScale,@level3ReturnScale)");
                            SqlParameter[] returnScaleParameters = {
                                new SqlParameter("@busnieseNum", SqlDbType.NVarChar,50),
                                new SqlParameter("@appNum", SqlDbType.NVarChar,50),
                                new SqlParameter("@templatesNum", SqlDbType.NVarChar,50),
                                new SqlParameter("@goodId", SqlDbType.BigInt,8),
                                new SqlParameter("@level1ReturnScale", SqlDbType.Decimal,9),
                                new SqlParameter("@level2ReturnScale", SqlDbType.Decimal,9),
                                new SqlParameter("@level3ReturnScale", SqlDbType.Decimal,9)};
                            returnScaleParameters[0].Value = model.busnieseNum;
                            returnScaleParameters[1].Value = model.appNum;
                            returnScaleParameters[2].Value = model.templatesNum;
                            returnScaleParameters[3].Value = goodId;
                            returnScaleParameters[4].Value = model.goodReturnScale.level1ReturnScale;
                            returnScaleParameters[5].Value = model.goodReturnScale.level2ReturnScale;
                            returnScaleParameters[6].Value = model.goodReturnScale.level3ReturnScale;

                            DbHelperSQL.GetSingle(strSqlReturnScale.ToString(), returnScaleParameters);
                        }
                        #endregion

                        trans.Commit();
                    }
                    catch
                    {
                        trans.Rollback();
                        return 0;
                    }
                }
            }
            return goodId;
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.bs_good_info model)
        {
            using (SqlConnection conn = new SqlConnection(DbHelperSQL.connectionString))
            {
                conn.Open();
                using (SqlTransaction trans = conn.BeginTransaction())
                {
                    try
                    {
                        StringBuilder strSql = new StringBuilder();
                        strSql.Append("update bs_good_info set ");
                        strSql.Append("busnieseNum=@busnieseNum,");
                        strSql.Append("appNum=@appNum,");
                        strSql.Append("templatesNum=@templatesNum,");
                        strSql.Append("typeNum=@typeNum,");
                        strSql.Append("goodTypeId=@goodTypeId,");
                        strSql.Append("name=@name,");
                        strSql.Append("description=@description,");
                        strSql.Append("stock=@stock,");
                        strSql.Append("costPrice=@costPrice,");
                        strSql.Append("sellPrice=@sellPrice,");
                        strSql.Append("deletePrice=@deletePrice,");
                        strSql.Append("sort=@sort,");
                        strSql.Append("status=@status,");
                        strSql.Append("goodHot=@goodHot,");
                        strSql.Append("lastTime=@lastTime,");
                        strSql.Append("lastUserId=@lastUserId,");
                        strSql.Append("showImage=@showImage,");
                        strSql.Append("zanCount=@zanCount");
                        strSql.Append(" where id=@id");
                        SqlParameter[] parameters = {
					        new SqlParameter("@busnieseNum", SqlDbType.NVarChar,50),
					        new SqlParameter("@appNum", SqlDbType.NVarChar,50),
					        new SqlParameter("@templatesNum", SqlDbType.NVarChar,50),
					        new SqlParameter("@typeNum", SqlDbType.NVarChar,50),
					        new SqlParameter("@goodTypeId", SqlDbType.BigInt,8),
					        new SqlParameter("@name", SqlDbType.NVarChar,50),
					        new SqlParameter("@description", SqlDbType.NVarChar,-1),
					        new SqlParameter("@stock", SqlDbType.Int,4),
					        new SqlParameter("@costPrice", SqlDbType.Decimal,9),
					        new SqlParameter("@sellPrice", SqlDbType.Decimal,9),
					        new SqlParameter("@deletePrice", SqlDbType.Decimal,9),
					        new SqlParameter("@sort", SqlDbType.Int,4),
					        new SqlParameter("@status", SqlDbType.NVarChar,50),
					        new SqlParameter("@goodHot", SqlDbType.NVarChar,50),
					        new SqlParameter("@lastTime", SqlDbType.DateTime),
					        new SqlParameter("@lastUserId", SqlDbType.BigInt,8),
					        new SqlParameter("@showImage", SqlDbType.NVarChar,-1),
					        new SqlParameter("@zanCount", SqlDbType.Int,4),
					        new SqlParameter("@id", SqlDbType.BigInt,8)};
                        parameters[0].Value = model.busnieseNum;
                        parameters[1].Value = model.appNum;
                        parameters[2].Value = model.templatesNum;
                        parameters[3].Value = model.typeNum;
                        parameters[4].Value = model.goodTypeId;
                        parameters[5].Value = model.name;
                        parameters[6].Value = model.description;
                        parameters[7].Value = model.stock;
                        parameters[8].Value = model.costPrice;
                        parameters[9].Value = model.sellPrice;
                        parameters[10].Value = model.deletePrice;
                        parameters[11].Value = model.sort;
                        parameters[12].Value = model.status;
                        parameters[13].Value = model.goodHot;
                        parameters[14].Value = model.lastTime;
                        parameters[15].Value = model.lastUserId;
                        parameters[16].Value = model.showImage;
                        parameters[17].Value = model.zanCount;
                        parameters[18].Value = model.id;

                        int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);

                        #region 图片
                        //先做删除处理
                        string strDeleteImage = string.Format("delete from bs_good_image where goodId={0}", model.id);
                        DbHelperSQL.ExecuteSql(conn, trans, strDeleteImage);

                        //后做添加处理
                        if (model.goodImages != null)
                        {
                            foreach (Model.bs_good_image goodImage in model.goodImages)
                            {
                                StringBuilder strSql2 = new StringBuilder();
                                strSql2.Append("insert into bs_good_image(");
                                strSql2.Append("busnieseNum,appNum,templatesNum,goodId,imageUrl,remark)");
                                strSql2.Append(" values (");
                                strSql2.Append("@busnieseNum,@appNum,@templatesNum,@goodId,@imageUrl,@remark)");
                                SqlParameter[] imageParameters = {
					                new SqlParameter("@busnieseNum", SqlDbType.NVarChar,50),
					                new SqlParameter("@appNum", SqlDbType.NVarChar,50),
					                new SqlParameter("@templatesNum", SqlDbType.NVarChar,50),
					                new SqlParameter("@goodId", SqlDbType.BigInt,8),
					                new SqlParameter("@imageUrl", SqlDbType.NVarChar,-1),
					                new SqlParameter("@remark", SqlDbType.NVarChar,-1)};
                                imageParameters[0].Value = goodImage.busnieseNum;
                                imageParameters[1].Value = goodImage.appNum;
                                imageParameters[2].Value = goodImage.templatesNum;
                                imageParameters[3].Value = model.id;
                                imageParameters[4].Value = goodImage.imageUrl;
                                imageParameters[5].Value = goodImage.remark;

                                DbHelperSQL.ExecuteSql(conn, trans, strSql2.ToString(), imageParameters);
                                strSql2.Length = 0;
                            }
                        }
                        #endregion

                        #region 商品扩展
                        //先做删除处理
                        string strDeleteExtend = string.Format("delete from bs_good_extend where goodId={0}", model.id);
                        DbHelperSQL.ExecuteSql(conn, trans, strDeleteExtend);

                        //后做添加处理
                        if (model.goodExtends != null)
                        {
                            foreach (Model.bs_good_extend goodExtend in model.goodExtends)
                            {
                                StringBuilder strSql3 = new StringBuilder();
                                strSql3.Append("insert into bs_good_extend(");
                                strSql3.Append("busnieseNum,appNum,templatesNum,goodId,goodTypeId,goodTypeFieldCode,fieldValue)");
                                strSql3.Append(" values (");
                                strSql3.Append("@busnieseNum,@appNum,@templatesNum,@goodId,@goodTypeId,@goodTypeFieldCode,@fieldValue)");
                                SqlParameter[] extendParameters = {
					                new SqlParameter("@busnieseNum", SqlDbType.NVarChar,50),
					                new SqlParameter("@appNum", SqlDbType.NVarChar,50),
					                new SqlParameter("@templatesNum", SqlDbType.NVarChar,50),
					                new SqlParameter("@goodId", SqlDbType.BigInt,8),
					                new SqlParameter("@goodTypeId", SqlDbType.BigInt,8),
					                new SqlParameter("@goodTypeFieldCode", SqlDbType.NVarChar,50),
					                new SqlParameter("@fieldValue", SqlDbType.NVarChar,-1)};
                                extendParameters[0].Value = goodExtend.busnieseNum;
                                extendParameters[1].Value = goodExtend.appNum;
                                extendParameters[2].Value = goodExtend.templatesNum;
                                extendParameters[3].Value = model.id;
                                extendParameters[4].Value = goodExtend.goodTypeId;
                                extendParameters[5].Value = goodExtend.goodTypeFieldCode;
                                extendParameters[6].Value = goodExtend.fieldValue;

                                DbHelperSQL.ExecuteSql(conn, trans, strSql3.ToString(), extendParameters);
                                strSql3.Length = 0;
                            }
                        }
                        #endregion

                        #region 商品返佣设置
                        if (model.goodReturnScale != null)
                        {
                            StringBuilder strSqlReturnScale = new StringBuilder();
                            strSqlReturnScale.Append("update bs_good_returnScale set ");
                            strSqlReturnScale.Append("busnieseNum=@busnieseNum,");
                            strSqlReturnScale.Append("appNum=@appNum,");
                            strSqlReturnScale.Append("templatesNum=@templatesNum,");
                            strSqlReturnScale.Append("level1ReturnScale=@level1ReturnScale,");
                            strSqlReturnScale.Append("level2ReturnScale=@level2ReturnScale,");
                            strSqlReturnScale.Append("level3ReturnScale=@level3ReturnScale");
                            strSqlReturnScale.Append(" where goodId=@goodId");
                            SqlParameter[] returnScaleParameters = {
					            new SqlParameter("@busnieseNum", SqlDbType.NVarChar,50),
					            new SqlParameter("@appNum", SqlDbType.NVarChar,50),
					            new SqlParameter("@templatesNum", SqlDbType.NVarChar,50),
					            new SqlParameter("@level1ReturnScale", SqlDbType.Decimal,9),
					            new SqlParameter("@level2ReturnScale", SqlDbType.Decimal,9),
					            new SqlParameter("@level3ReturnScale", SqlDbType.Decimal,9),
					            new SqlParameter("@goodId", SqlDbType.BigInt,8)};
                            returnScaleParameters[0].Value = model.busnieseNum;
                            returnScaleParameters[1].Value = model.appNum;
                            returnScaleParameters[2].Value = model.templatesNum;
                            returnScaleParameters[3].Value = model.goodReturnScale.level1ReturnScale;
                            returnScaleParameters[4].Value = model.goodReturnScale.level2ReturnScale;
                            returnScaleParameters[5].Value = model.goodReturnScale.level3ReturnScale;
                            returnScaleParameters[6].Value = model.id;

                            DbHelperSQL.ExecuteSql(strSqlReturnScale.ToString(), returnScaleParameters);
                        }
                        #endregion
                    }
                    catch
                    {
                        trans.Rollback();
                        return false;
                    }
                }
            }

            return true;
        }

        /// <summary>
        /// 删除一条数据
        /// 同时删除关联数据：
        ///     图片（含物理文件）
        ///     扩展信息
        ///     评论
        ///     返佣设置
        /// </summary>
        public bool Delete(long id)
        {
            using (SqlConnection conn = new SqlConnection(DbHelperSQL.connectionString))
            {
                conn.Open();
                using (SqlTransaction trans = conn.BeginTransaction())
                {
                    try
                    {
                        Model.bs_good_info goodInfo = this.GetModel(id);

                        StringBuilder strSql = new StringBuilder();
                        strSql.Append("delete from bs_good_info ");
                        strSql.Append(" where id=@id");
                        SqlParameter[] parameters = {
					        new SqlParameter("@id", SqlDbType.BigInt)
			            };
                        parameters[0].Value = id;

                        int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
                        if (rows > 0)
                        {
                            //var goodImageList = 

                            //删除图片（表数据）
                            string deleteSql = string.Format("delete from bs_good_image where goodId='{0}'", id);
                            DbHelperSQL.ExecuteSql(deleteSql);
                            //删除封面图片物理文件
                            DAL.bs_good_image goodImageDal = new bs_good_image();
                            if (goodInfo != null)
                            {
                                goodImageDal.DeleteFile(goodInfo.showImage);
                            }
                            //删除商品图片列表物理文件
                            goodImageDal.DeleteGoodImageFiles(id);

                            //删除扩展信息
                            deleteSql = string.Format("delete from bs_good_extend where goodId='{0}'", id);
                            DbHelperSQL.ExecuteSql(deleteSql);

                            //删除评论
                            deleteSql = string.Format("delete from bs_good_comment where goodId='{0}'", id);
                            DbHelperSQL.ExecuteSql(deleteSql);

                            //删除返佣设置
                            deleteSql = string.Format("delete from bs_good_returnScale where goodId='{0}'", id);
                            DbHelperSQL.ExecuteSql(deleteSql);
                        }
                    }
                    catch
                    {
                        trans.Rollback();
                        return false;
                    }
                }
            }
            return true;
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from bs_good_info ");
            strSql.Append(" where id in (" + idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.bs_good_info GetModel(long id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,busnieseNum,appNum,templatesNum,typeNum,goodTypeId,name,description,stock,costPrice,sellPrice,deletePrice,sort,status,goodHot,lastTime,lastUserId,showImage,zanCount from bs_good_info ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.BigInt)
			};
            parameters[0].Value = id;

            Model.bs_good_info model = new Model.bs_good_info();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public SF.Model.bs_good_info GetModel(string where)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,busnieseNum,appNum,templatesNum,typeNum,goodTypeId,name,description,stock,costPrice,sellPrice,deletePrice,sort,status,goodHot,lastTime,lastUserId,showImage,zanCount from bs_good_info ");
            if (where != "")
            {
                strSql.Append(" where " + where);
            }
            DataSet ds = DbHelperSQL.Query(strSql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.bs_good_info DataRowToModel(DataRow row)
        {
            Model.bs_good_info model = new Model.bs_good_info();
            if (row != null)
            {
                if (row["id"] != null && row["id"].ToString() != "")
                {
                    model.id = long.Parse(row["id"].ToString());
                }
                if (row["busnieseNum"] != null)
                {
                    model.busnieseNum = row["busnieseNum"].ToString();
                }
                if (row["appNum"] != null)
                {
                    model.appNum = row["appNum"].ToString();
                }

                if (row["templatesNum"] != null)
                {
                    model.templatesNum = row["templatesNum"].ToString();
                }
                if (row["typeNum"] != null)
                {
                    model.typeNum = row["typeNum"].ToString();
                }
                if (row["goodTypeId"] != null && row["goodTypeId"].ToString() != "")
                {
                    model.goodTypeId = long.Parse(row["goodTypeId"].ToString());
                }
                if (row["name"] != null)
                {
                    model.name = row["name"].ToString();
                }
                if (row["description"] != null)
                {
                    model.description = row["description"].ToString();
                }
                if (row["stock"] != null && row["stock"].ToString() != "")
                {
                    model.stock = int.Parse(row["stock"].ToString());
                }
                if (row["costPrice"] != null && row["costPrice"].ToString() != "")
                {
                    model.costPrice = decimal.Parse(row["costPrice"].ToString());
                }
                if (row["sellPrice"] != null && row["sellPrice"].ToString() != "")
                {
                    model.sellPrice = decimal.Parse(row["sellPrice"].ToString());
                }
                if (row["deletePrice"] != null && row["deletePrice"].ToString() != "")
                {
                    model.deletePrice = decimal.Parse(row["deletePrice"].ToString());
                }
                if (row["sort"] != null && row["sort"].ToString() != "")
                {
                    model.sort = int.Parse(row["sort"].ToString());
                }
                if (row["status"] != null)
                {
                    model.status = row["status"].ToString();
                }
                if (row["goodHot"] != null)
                {
                    model.goodHot = row["goodHot"].ToString();
                }
                if (row["lastTime"] != null && row["lastTime"].ToString() != "")
                {
                    model.lastTime = DateTime.Parse(row["lastTime"].ToString());
                }
                if (row["lastUserId"] != null && row["lastUserId"].ToString() != "")
                {
                    model.lastUserId = long.Parse(row["lastUserId"].ToString());
                }
                if (row["showImage"] != null)
                {
                    model.showImage = row["showImage"].ToString();
                }
                if (row["zanCount"] != null && row["zanCount"].ToString() != "")
                {
                    model.zanCount = int.Parse(row["zanCount"].ToString());
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,busnieseNum,appNum,templatesNum,typeNum,goodTypeId,name,description,stock,costPrice,sellPrice,deletePrice,sort,status,goodHot,lastTime,lastUserId,showImage,zanCount ");
            strSql.Append(" FROM bs_good_info ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" id,busnieseNum,appNum,templatesNum,typeNum,goodTypeId,name,description,stock,costPrice,sellPrice,deletePrice,sort,status,goodHot,lastTime,lastUserId,showImage,zanCount ");
            strSql.Append(" FROM bs_good_info ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM bs_good_info ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.id desc");
            }
            strSql.Append(")AS Row, T.*  from bs_good_info T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "bs_good_info";
            parameters[1].Value = "id";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  BasicMethod
        #region  ExtensionMethod
        /// <summary>
        /// 获得查询分页数据
        /// </summary>
        public DataSet GetList(string status, string goodTypeId, int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * FROM bs_good_info where 1=1");
            strSql.Append(" and status='" + status + "'");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" and  " + strWhere);
            }
            if (!String.IsNullOrEmpty(goodTypeId.Trim()))
            {
                //需要读取下级的商品，故此处需要递归
                DAL.bs_good_type goodTypeDal = new DAL.bs_good_type();
                DataTable dt = goodTypeDal.GetList("class_list like '%" + goodTypeDal.GetModel(Convert.ToInt64(goodTypeId)).class_list + "%'").Tables[0];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (i == 0)
                    {
                        strSql.Append(" and  goodTypeId='" + dt.Rows[i]["id"] + "' ");
                    }
                    else
                    {
                        strSql.Append(" or  goodTypeId='" + dt.Rows[i]["id"] + "' ");
                    }
                }
            }
            recordCount = Convert.ToInt32(DbHelperSQL.GetSingle(PagingHelper.CreateCountingSql(strSql.ToString())));
            return DbHelperSQL.Query(PagingHelper.CreatePagingSql(recordCount, pageSize, pageIndex, strSql.ToString(), filedOrder));
        }

        /// <summary>
        /// 修改一列数据
        /// </summary>
        public bool UpdateField(long id, string strValue)
        {
            try
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("update bs_good_info set " + strValue);
                strSql.Append(" where id=" + id);
                DbHelperSQL.ExecuteSql(strSql.ToString());
                return true;
            }
            catch (Exception)
            {
            }
            return false;
        }

        /// <summary>
        /// 获取最大排序数
        /// </summary>
        public int GetMaxSort(string strWhere)
        {
            int result = 1;
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select MAX(sort) from bs_good_info");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj != null)
            {
                result = Convert.ToInt32(obj);
            }
            return result;
        }
        #endregion  ExtensionMethod
    }
}
